package com.moonshineproject.plugin.templating
{
	import com.moonshineproject.utils.FileUtil;
	import com.moonshineproject.utils.TextUtil;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	public class TemplatingHelper
	{
		// Replace values for templates {$ProjectName:"My New Project"}
		public var templatingData:Object = {};
		
		public function fileTemplate(fromTemplate:File, toFile:File):void
		{
			FileUtil.createFile(toFile);
			
			copyFileTemplate(fromTemplate, toFile);
		}
		
		public function projectTemplate(fromDir:File, toDir:File):void
		{
			copyFiles(fromDir, toDir);
		}
		
		private function copyFiles(fromDir:File, toDir:File):void
		{
			var files:Array = fromDir.getDirectoryListing();
			var newFile:File;
			var template:Boolean;
			
			for each (var file:File in files)
			{
				if (file.isDirectory)
				{
					newFile = toDir.resolvePath( templatedFileName(file) );
					newFile.createDirectory();
					
					copyFiles(file, newFile);
				}
				else
				{
					template = (file.name.indexOf(".template") > -1);
					
					newFile = toDir.resolvePath( templatedFileName(file) );
					
					if (template) copyFileTemplate(file, newFile);
					else copyFileContents(file, newFile);
				}
			}
		}
		
		private function templatedFileName(src:File):String
		{
			var name:String = src.name;
			if (name.indexOf("$") > -1)
			{
				var m:int;
				for (var key:String in templatingData)
				{
					m = name.indexOf(key);	
					if (m > -1)
					{
						name = name.substr(0, m) + templatingData[key] + name.substr(m+key.length); 
					}
				}
			}
			
			if (name.indexOf(".template") > -1)
			{
				name = name.substr(0, name.indexOf(".template"));
			}
			
			return name;
		}
		
		private function copyFileContents(src:File, dst:File):void
		{
			src.copyTo(dst);
		}
		
		private function copyFileTemplate(src:File, dst:File):void
		{
			var content:String;
			var r:FileStream = new FileStream();
			r.open(src, FileMode.READ);
			content = r.readUTFBytes(src.size);
			r.close();
			
			content = replace(content, templatingData);
			
			var w:FileStream = new FileStream();
			w.open(dst, FileMode.WRITE);
			w.writeUTFBytes(content);
			w.close();
		}
		
		public static function replace(content:String, data:Object):String
		{
			for (var key:String in data)
			{
				var re:RegExp = new RegExp(TextUtil.escapeRegex(key), "g");
				content = content.replace(re, data[key]);
			}
			
			return content;
		}
		
		public static function getTemplateLabel(template:File):String
		{
			var name:String = template.name;
			name = stripTemplate(name);
			
			if (name.indexOf(".") > -1)
			{
				name = name.substr(0, name.indexOf("."));
			}
			
			return name;
		}
		
		public static function stripTemplate(from:String):String
		{
			if (from.indexOf(".template") > -1)
			{
				from = from.substr(0, from.indexOf(".template"));
			}
			
			return from;
		}
		
		public static function getExtension(template:File):String
		{
			var name:String = stripTemplate(template.name);
			
			if (name.lastIndexOf(".") > -1)
			{
				return name.substr( name.lastIndexOf(".")+1 );	
			}
			
			return null;
		}
		
	}
}